<?php

require_once 'Ext/User/Action/Edit/Email/Interface.php';

abstract class Ext_User_Action_Edit_Email_Abstract implements Ext_User_Action_Edit_Email_Interface
{
    protected $_userId;
    protected $_messager;

    const MESSAGE_SUCCESS                         = 'changeEmailSuccess';
    const MESSAGE_FAILURE                         = 'changeEmailFailure';
    const MESSAGE_FAILURE_RECORD_NOT_FOUND        = 'RecordNotFound';
    const MESSAGE_FAILURE_EMAIL_FORMAT_ERROR      = 'emailFormatError';
    const MESSAGE_FAILURE_EMAIL_IS_DENIED         = 'emailIsDenied';
    const MESSAGE_FAILURE_EMAIL_ALREADY_EXISTS    = 'emailAlreadyExists';
    const MESSAGE_FAILURE_USER_IS_PROTECT         = 'userIsProtect';
    const MESSAGE_FAILURE_UNCATEGORIZED           = 'uncategorized';
    
    protected $_messageTemplates = array(
        self::MESSAGE_SUCCESS                        => "Change email success.",
        self::MESSAGE_FAILURE                        => "Change email failed.",
        self::MESSAGE_FAILURE_RECORD_NOT_FOUND       => "Record not found.",
        self::MESSAGE_FAILURE_EMAIL_FORMAT_ERROR     => "Email format error.",
        self::MESSAGE_FAILURE_EMAIL_IS_DENIED        => "Email is denied.",
        self::MESSAGE_FAILURE_EMAIL_ALREADY_EXISTS   => "Email already exists.",
        self::MESSAGE_FAILURE_USER_IS_PROTECT        => "The user is protect.",
        self::MESSAGE_FAILURE_UNCATEGORIZED          => "Uncategorized.",
    );
    
    public function __construct($userId)
    {
        $this->_userId     = $userId;
        
        $this->_messager = new Ext_Messager($this->_messageTemplates);
    }
    
    public function setResult($code)
    {
        $result = array(
            'code'         => Ext_User_Action_Edit_Password_Result::FAILURE,
            'messages'     => array(),
        );
	
        if($code == Ext_User_Action_Edit_Email_Result::SUCCESS){
            $result['messages'][] = $this->_messager->addMessage(self::MESSAGE_SUCCESS);
        } else{
            $_message = $this->_messager->addMessage(self::MESSAGE_FAILURE);
            
            if($code == Ext_User_Action_Edit_Email_Result::FAILURE_RECORD_NOT_FONUD){
                $_message .= $this->_messager->addMessage(self::MESSAGE_FAILURE_RECORD_NOT_FOUND);
            }
            elseif($code == Ext_User_Action_Edit_Email_Result::FAILURE_EMAIL_FORMAT_ERROR){
                $_message .= $this->_messager->addMessage(self::MESSAGE_FAILURE_EMAIL_FORMAT_ERROR);
            }
            elseif($code == Ext_User_Action_Edit_Email_Result::FAILURE_EMAIL_IS_DENIED){
                $_message .= $this->_messager->addMessage(self::MESSAGE_FAILURE_EMAIL_IS_DENIED);
            }
            elseif($code == Ext_User_Action_Edit_Email_Result::FAILURE_EMAIL_ALREADY_EXISTS){
                $_message .= $this->_messager->addMessage(self::MESSAGE_FAILURE_EMAIL_ALREADY_EXISTS);
            }
            elseif($code == Ext_User_Action_Edit_Email_Result::FAILURE_USER_IS_PROTECTED){
                $_message .= $this->_messager->addMessage(self::MESSAGE_FAILURE_USER_IS_PROTECT);
            }
            elseif($code == Ext_User_Action_Edit_Email_Result::FAILURE_UNCATEGORIZED){
                $_message .= $this->_messager->addMessage(self::MESSAGE_FAILURE_UNCATEGORIZED);
            }
            
            $result['messages'][] = $_message;
        }
		
        $this->_result = new Ext_User_Action_Edit_Password_Result($code, $result['messages']);

    }
    
    public function getResult()
    {
        return $this->_result;
    }
}
